package com.irdstudio.efp.esb.api.rest;

import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.esb.api.common.GroupeInterface;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;

import java.util.Objects;

/**
 * Controller 接口定义通用方法: <br>
 * <p>
 * 0.1:caoxin:2019/4/18 17:29: TODO: <br>
 *
 * @author caoxin
 * @version 0.1
 * @date 2019/4/18 17:29
 * @since 2019/4/18 17:29
 */
public interface EsbController {
    /**
     * 参数校验: <br>
     *
     * 0.1:caoxin:2019/4/18 17:33: TODO: <br>
     * @author caoxin
     * @return
     * @since 2019/4/18 17:33
     * @date 2019/4/18 17:33
     * @version 0.1
     */

    default void validateBean(@Validated EsbReqServiceBean esbReqServiceBean, BindingResult bindingResult1, @Validated({GroupeInterface.sed.class}) Object object, BindingResult bindingResult2, Exception exception, Logger logger) throws ESBException {
        // 判断参数是否校验成功
        String bindingResultOfEsbBeanMsg = bindingResult1.hasErrors() ? bindingResult1.getFieldError().getDefaultMessage() : "参数校验成功";
        String bindingResultOfCreditBeanMsg = bindingResult2.hasErrors() ? bindingResult2.getFieldError().getDefaultMessage() : "参数校验成功";

        logger.info(bindingResultOfEsbBeanMsg);
        logger.info(bindingResultOfCreditBeanMsg);

        // 判断接收两个bean是否为空
        Objects.requireNonNull(esbReqServiceBean);
        Objects.requireNonNull(object);
        // 如果有错误则直接抛出异常
        if (Objects.nonNull(exception)){
            throw new ESBException(EsbRetCodeStandard.JKYDCSJYBTG.getValue(),exception.getMessage());
        }
    }
    /**
     * 将系统头放入ThreadLocal: <br>
     *
     * 0.1:caoxin:2019/4/18 17:53: TODO: <br>
     * @author caoxin
     * @return
     * @since 2019/4/18 17:53
     * @date 2019/4/18 17:53
     * @version 0.1
     */

    default void setSysHeadFieldThreadLocal(EsbReqServiceBean esbReqServiceBean) {
        // 清空全局流水号
        TraceUtil.clear();
        // 获取全局关联流水号
        String glblSrlNo = esbReqServiceBean.getSysHead().getGlblSrlNo();
        if (StringUtils.isNotEmpty(glblSrlNo)) {
            TraceUtil.setTraceId(glblSrlNo);
        }
        // 获取平台ID
        String plafmID = esbReqServiceBean.getSysHead().getPlafmID();
        if (StringUtils.isNotEmpty(plafmID)) {
            TraceUtil.setPlafmID(plafmID);
        }
        // 获取平台用户ID
        String plafmUsrID = esbReqServiceBean.getSysHead().getPlafmUsrID();
        if (StringUtils.isNotEmpty(plafmUsrID)) {
            TraceUtil.setPlafmUsrID(plafmUsrID);
        }
    }

    ///**
    // * 获取ESB业务请求对象: <br>
    // *
    // * 0.1:caoxin:2019/4/18 17:35: TODO: <br>
    // * @author caoxin
    // * @return
    // * @since 2019/4/18 17:35
    // * @date 2019/4/18 17:35
    // * @version 0.1
    // */
    //@Deprecated
    //default EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
    //    EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
    //    return esbReqServiceBean;
    //}
    ///**
    // * 获取请求体BODY: <br>
    // *
    // * 0.1:caoxin:2019/4/18 17:36: TODO: <br>
    // * @author caoxin
    // * @return
    // * @since 2019/4/18 17:36
    // * @date 2019/4/18 17:36
    // * @version 0.1
    // */
    //@Deprecated
    //default T getReqEsbBodyBean(HttpServletRequest request, T t) {
    //    String esbReqBody = (String) request.getAttribute("esbReqBody");
    //    return JSONObject.parseObject(esbReqBody,t.getClass());
    //}


}
